class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def nextLargerNodes(self, head):
        """
        :type head: ListNode
        :rtype: List[int]
        """
        nums = []
        node = head
        while node:
            nums.append(node.val)
            node = node.next
        n = len(nums)
        st = []
        out = [0] * n
        for i in range(n):
            while st and nums[i] > nums[st[-1]]:
                out[st.pop()] = nums[i]
            st.append(i)
        return out

